package com.org.testproject;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccelerometerTest extends Activity implements SensorEventListener{

	TextView textView;
	StringBuilder builder = new StringBuilder();
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		textView = new TextView(this);
		setContentView(textView);
		SensorManager manager = 
			(SensorManager)getSystemService(Context.SENSOR_SERVICE);
		
		if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0)
		{
			textView.setText("No accelerometer installed");
		}
		else
		{
			Sensor accelerometer = manager.getSensorList(
						Sensor.TYPE_ACCELEROMETER
					).get(0);
			
			if(!manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME))
			{
				textView.setText("Couldn't register sensor listener");
			}
		}
	}
	
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		// do nothing...
	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		builder.setLength(0);
		builder.append("x: ");
		builder.append(arg0.values[0]);
		builder.append(", y: ");
		builder.append(arg0.values[1]);
		builder.append(", z: ");
		builder.append(arg0.values[2]);
		textView.setText(builder.toString());
	}

}
